<?php
namespace app\admin\controller;
use mylib\Delimg;


class Varieties extends Init
{
    public function index()
    {
        $varieties = db('varieties')->order('v_id','desc')->paginate(5);
        $this->assign("varieties", $varieties);
        return view();
    }

    public function add()
    {

        if(request()->isPost()) {
            $data['petname'] = input('petname');
            $data['v_name'] = input('v_name');
            $data['v_title'] = input('v_title');
            $data['v_content'] = input('v_content');

            $files = request()->file('image');
            $imgarr = [];
            foreach ($files as $file){
                $upload_path = config('upload.path');
                $info = $file->rule('uniqid')->move($upload_path);

                if($info){
                    $imgarr[] = $info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }
            $data['v_img'] = $imgarr[0];
            $data['v_img_con'] = $imgarr[1];


            $result = $this->validate($data,'Varieties');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
            }

            if(db('varieties')->insert($data)){
                $this->redirect('varieties/index');
            }else{
                return $this->error('添加失败！');
            }


        }

    }

    public function edit(){
        $id = input('id');
        $varieties = db('varieties')->where('v_id',$id)->find();
        $this->assign("varieties", $varieties);

        if(request()->isPost()){
            $data['petname'] = input('petname');
            $data['v_name'] = input('v_name');
            $data['v_title'] = input('v_title');
            $data['v_content'] = input('v_content');

            //删除原图片
//            $delimg = new Delimg();
//            $delimg->delupimg('varieties','v_id',$id,'v_img',".jpg",'_150_200.jpg');
//            $delimg->delupimg('varieties','v_id',$id,'v_img_con',".jpg",'_150_200.jpg');



            //增加新的图片
            $files = request()->file('image');
            if($files){
                $imgarr = [];
                foreach ($files as $file){

                    $upload_path = config('upload.path');
                    $info = $file->rule('uniqid')->move($upload_path);

                    if($info){
                        $imgarr[] = $info->getSaveName();
                    }else{
                        // 上传失败获取错误信息
                        echo $file->getError();
                    }
                }


                $data['v_img'] = $imgarr[0];
                $data['v_img_con'] = $imgarr[1];


                if(db('varieties')->where('v_id',$id)->update($data)){
                    $this->redirect('varieties/index');
                }else{
                    return $this->error('修改失败！');
                }
            }else{
                $this->error('请选择上传图片');
            }

        }
        return view();
    }

    public function del(){
        $id = input('id');
        $delimg = new Delimg();
        $delimg->delupimg('varieties','v_id',$id,'v_img',".jpg",'_150_200.jpg');
        $delimg->delupimg('varieties','v_id',$id,'v_img_con',".jpg",'_150_200.jpg');


        if(db('varieties')->where('v_id',$id)->delete()){
            $this->redirect('varieties/index');
        }else{
            return $this->error('删除失败！');
        }
    }

    public function delall(){
        $data = input('post.');

        $arrdel = $data['idarr'];
        $delimg = new Delimg();
        $delimg->delAllimg('varieties','v_id',$arrdel,'v_img',".jpg",'_150_200.jpg');

        if(request()->isPost()) {
            foreach ($arrdel as $id){
                db('varieties')->where('v_id',$id)->delete();
            }
            $this->redirect('varieties/index');
        }
    }

}